package www.woniuxy.com.class112.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import www.woniuxy.com.class112.dto.*;
import www.woniuxy.com.class112.entity.Issues;
import www.woniuxy.com.class112.form.*;
import www.woniuxy.com.class112.form.workOrdes.AdminselectWorkOrdersForm;
import www.woniuxy.com.class112.param.*;
import www.woniuxy.com.class112.param.workOrdes.AdminselectWorkOrdersParam;
import www.woniuxy.com.class112.service.WorkOrdersService;
import www.woniuxy.com.class112.util.JwtUtil;
import www.woniuxy.com.class112.util.Result;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/workOrders")
@Api(tags = "用户提问相关接口")
public class WorkOrdersController {
    @Resource
    private WorkOrdersService workOrdersService;
    @PostMapping("/agriculturalIssues")
    @ApiOperation("用户向知识库提问")
    public Result agriculturalIssues(
            @RequestParam("description") String description,
            @RequestParam("issueType") Long issueType,
            @RequestParam("image") List<MultipartFile> image,  // 确保字段名一致
            @RequestHeader String token) throws Exception {

        // 输出日志查看是否接收到了文件
        System.out.println("接收到的文件数: " + image.size());
        if (image.isEmpty()) {
            throw new Exception("没有上传任何文件");
        }



        AgriculturalIssuesParam param = new AgriculturalIssuesParam();
        param.setDescription(description);
        param.setIssueType(issueType);
        param.setImage(image);

        Long userId = JwtUtil.getUserId(token);
        param.setUserId(userId);

        QusDto qus = workOrdersService.agriculturalIssues(param);

        return Result.success("提问成功", qus);
    }




    @GetMapping("/knowledgeBaseSolvesProblems")
    @ApiOperation("知识库已解决问题接口")
    public Result knowledgeBaseSolvesProblems(Long issuesId,@RequestHeader String token){
        Long userId = JwtUtil.getUserId(token);
        workOrdersService.knowledgeBaseSolvesProblems(issuesId,userId);
        return Result.success("知识库已解决问题",null);
    }

    @GetMapping("/theUserIsLookingForASpecialEnvoy")
    @ApiOperation("用户查完知识库之后找特派员")
    public Result theUserIsLookingForASpecialEnvoy(Long issuesId){
        workOrdersService.theUserIsLookingForASpecialEnvoy(issuesId);
        return Result.success("提交问题表成功",null);
    }

    @GetMapping("/userQueryQuestionTable")
    @ApiOperation("根据用户自身的id查询自己的全部提问表")
    public Result userQueryQuestionTable(@RequestHeader String token,Long issueState) throws Exception {
        Long userId = JwtUtil.getUserId(token);
        List<Issues> issues = workOrdersService.userQueryQuestionTable(userId,issueState);
        return Result.success("查询问题表成功",issues);
    }

    @PostMapping("/specialEnvoyAcceptsOrders")
    @ApiOperation("特派员接取用户问题表")
    public Result specialEnvoyAcceptsOrders(@RequestBody specialEnvoyAcceptsOrdersForm form,@RequestHeader String token) throws Exception {
        Long agentId = JwtUtil.getUserId(token);
        specialEnvoyAcceptsOrdersParam param = BeanUtil.toBean(form, specialEnvoyAcceptsOrdersParam.class);
        param.setAgentId(agentId);
        workOrdersService.specialEnvoyAcceptsOrders(param);
        return Result.success("接单成功",null);
    }
    @PostMapping("/fillInTheWorkOrderAddress")
    @ApiOperation("特派员接单之后通过issuesId表来添加地址")
    public Result fillInTheWorkOrderAddress(@RequestBody FillInTheWorkOrderAddressForm form,@RequestHeader String token) throws Exception {
        Long userId = JwtUtil.getUserId(token);
        FillInTheWorkOrderAddressParam param = BeanUtil.toBean(form, FillInTheWorkOrderAddressParam.class);
        param.setUserId(userId);
        workOrdersService.fillInTheWorkOrderAddress(param);
        return Result.success("添加地址成功",null);
    }
    @PostMapping("/completeTheWorkOrder")
    @ApiOperation("线下办结工单")
    public Result completeTheWorkOrder(CompleteTheWorkOrderForm form) throws Exception {
        workOrdersService.completeTheWorkOrder(BeanUtil.toBean(form, CompleteTheWorkOrderParam.class));
        return Result.success("特派员结束流程",null);
    }

    @GetMapping("/specialEnvoyQueriesWorkOrders")
    @ApiOperation("特派员查询自己的工单")
    private Result specialEnvoyQueriesWorkOrders(@RequestHeader String token){
        Long agentId = JwtUtil.getUserId(token);
        List<SpecialEnvoyQueriesWorkOrdersDto> dtos = workOrdersService.specialEnvoyQueriesWorkOrders(agentId);
        return Result.success("特派员查询成功",dtos);
    }

    @GetMapping("/startOnlineWorkOrder")
    @ApiOperation("特派员开始线上工单")
    public Result startOnlineWorkOrder (Long id){
        workOrdersService.startOnlineWorkOrder(id);
        return Result.success("开启线上成功",null);
    }
    @PostMapping("/startOfflineWorkOrder")
    @ApiOperation("特派员开始线下工单")
    public Result startOfflineWorkOrder(@RequestBody StartOfflineWorkOrderForm form){
        workOrdersService.startOfflineWorkOrder(BeanUtil.toBean(form,StartOfflineWorkOrderParam.class));
        return Result.success("开启线下工单成功",null);
    }
    @PostMapping("/specialEnvoyOnlineStatement")
    @ApiOperation("特派员线上结单")
    public Result specialEnvoyOnlineStatement(@RequestBody specialEnvoyOnlineStatementForm form){
        workOrdersService.specialEnvoyOnlineStatement(BeanUtil.toBean(form, specialEnvoyOnlineStatementParam.class));
        return Result.success("特派员线上结单成功",null);
    }

    @PostMapping("/pendingEvaluationWorkOrder")
    @ApiOperation("用户搜索待评价的工单")
    public Result pendingEvaluationWorkOrder(@RequestBody PageForm form, @RequestHeader String token) throws Exception {
        Long userId = JwtUtil.getUserId(token);
        pendingEvaluationWorkOrderParam param = BeanUtil.toBean(form, pendingEvaluationWorkOrderParam.class);
        param.setUserId(userId);
        PageInfo<pendingEvaluationWorkOrderDto> pendingEvaluationWorkOrderDtos = workOrdersService.pendingEvaluationWorkOrder(param);
        return Result.success("搜索成功",pendingEvaluationWorkOrderDtos);
    }

    @PostMapping("/confirmEvaluation")
    @ApiOperation("立即评价")
    public Result confirmEvaluation(@RequestBody ConfirmEvaluationForm form){
        workOrdersService.confirmEvaluation(BeanUtil.toBean(form,ConfirmEvaluationParam.class));
        return Result.success("评价成功",null);
    }

    @PostMapping("/queryQuestions")
    @ApiOperation("根据前端传过来的问题状态查询问题表")
    public Result queryQuestions(@RequestBody PageForm form, @RequestHeader String token) throws Exception {
        Long userId = JwtUtil.getUserId(token);
        PageInfo<QueryQuestionsDto> dto =  workOrdersService.queryQuestions(BeanUtil.toBean(form,PageParam.class),userId);
        return Result.success("查询成功",dto);
    }

    @PostMapping("/cancelTheIssue")
    @ApiOperation("用户取消问题表")
    public Result cancelTheIssue(@RequestBody CancelTheIssueForm form,@RequestHeader String token){
        Long userId = JwtUtil.getUserId(token);
        CancelTheIssueParam param = BeanUtil.toBean(form, CancelTheIssueParam.class);
        param.setUserId(userId);
        workOrdersService.cancelTheIssue(param);
        return Result.success("取消成功",null);
    }

    @PostMapping("/queryProcessingInProgress")
    @ApiOperation("用户查询处理中的问题表")
    public Result queryProcessingInProgress(@RequestBody PageForm form,@RequestHeader String token){
        Long userId = JwtUtil.getUserId(token);
        PageInfo<QueryProcessingInProgressDto> dto =  workOrdersService.queryProcessingInProgress(BeanUtil.toBean(form,PageParam.class),userId);
        return Result.success("查询成功",dto);
    }
    @PostMapping("/AdminSelectWorkOrdes")
    @ApiOperation("管理员查询工单")
    public Result AdminSelectWorkOrdes(@RequestBody AdminselectWorkOrdersForm form){
        return Result.success("查询成功",workOrdersService.adminselectWorkOrdes(BeanUtil.toBean(form, AdminselectWorkOrdersParam.class)));
    }

    @PostMapping("/onlineCompletionOfWorkOrders")
    @ApiOperation("线上办结工单")
    public Result onlineCompletionOfWorkOrders(@RequestBody OnlineWorkOrderForm form){
        workOrdersService.onlineCompletionOfWorkOrders(BeanUtil.toBean(form,OnlineWorkOrderParam.class));
        return Result.success("办结成功",null);
    }

    @PostMapping("/specialEnvoyCancelled")
    @ApiOperation("特派员取消工单")
    public Result specialEnvoyCancelled(@RequestBody SpecialEnvoyCancelledForm form,@RequestHeader String token){
        Long agentId = JwtUtil.getUserId(token);
        SpecialEnvoyCancelledParam param = BeanUtil.toBean(form, SpecialEnvoyCancelledParam.class);
        param.setAgentId(agentId);
        workOrdersService.specialEnvoyCancelled(param);
        return Result.success("取消成功",null);
    }

    @PostMapping("/reportToTheMunicipalBureau")
    @ApiOperation("上报市局")
    public Result reportToTheMunicipalBureau(@RequestBody ReportToTheMunicipalBureauForm form){
        workOrdersService.reportToTheMunicipalBureau(BeanUtil.toBean(form,ReportToTheMunicipalBureauParam.class));
        return Result.success("上报成功",null);
    }
}

